package com.sohu.pickoo.frag;

import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.Date;

import org.quartz.CronTrigger;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;

import com.sohu.peak.util.ConfigManager;
import com.sohu.peak.util.Logger;

public class AllFragServer {

	public void run() throws Exception {
		Logger log = new Logger();

		log.debug("------- Initializing -------------------");

		// First we must get a reference to a scheduler
		SchedulerFactory sf = new StdSchedulerFactory();
		Scheduler sched = sf.getScheduler();

		log.debug("------- Initialization Complete --------");

		log.debug("------- Connecting Database ----------------");

		java.sql.Connection lConn = null;
		java.sql.Statement lStat = null;
		java.sql.ResultSet lRs = null;

		String driverClassName = ConfigManager.instance().getProperty(
				"newfrag", "pickooDB.dirver");

		DriverManager.registerDriver((Driver) Class.forName(driverClassName)
				.newInstance());

		String lUrl = ConfigManager.instance().getProperty("newfrag",
				"pickooDB.url");
		String user = ConfigManager.instance().getProperty("newfrag",
				"pickooDB.user");
		String passwd = ConfigManager.instance().getProperty("newfrag",
				"pickooDB.passwd");
		lConn = DriverManager.getConnection(lUrl, user, passwd);
		lStat = lConn.createStatement();

		log.debug("------- Connection Complete --------");

		log.debug("------- Getting Jobs From Database----------------");

		ResultSet rs = lStat.executeQuery("select * from FRAG where is_stop=0");

		log.debug("------- Get Jobs Complete----------------");

		log.debug("------- Scheduling Jobs ----------------");
		// jobs can be scheduled before sched.start() has been called
		if (rs != null) {
			while (rs.next()) {
				log.debug("------- Job " + rs.getString("id")
						+ "----------------");
				JobDetail job = new JobDetail("job_" + rs.getString("id"),
						"group1", FragJob.class);
				JobDataMap dataMap = new JobDataMap();
				dataMap.put("source", rs.getString("source"));
				dataMap.put("target", rs.getString("target"));
				job.setJobDataMap(dataMap);
				CronTrigger trigger = new CronTrigger("trigger_"
						+ rs.getString("id"), "group1", "job_"
						+ rs.getString("id"), "group1", "1 */1 * * * ?");
				sched.addJob(job, true);
				Date ft = sched.scheduleJob(trigger);
				log.debug(job.getFullName() + " has been scheduled to run at: "
						+ ft + " and repeat based on expression: "
						+ trigger.getCronExpression());
			}

		}

		log.debug("------- Starting Scheduler ----------------");

		// All of the jobs have been added to the scheduler, but none of the
		// jobs
		// will run until the scheduler has been started
		sched.start();

		log.debug("------- Started Scheduler -----------------");

		log.debug("------- Waiting For Executing... ------------");
		try {
			while (true) {
				// wait five minutes for executing
				Thread.sleep(300L * 1000L);
				// executing...
			}
		} catch (Exception e) {
		}

	}

	public static void main(String[] args) throws Exception {

		AllFragServer server = new AllFragServer();
		server.run();
	}
}
